package com.woniu.carrent.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.woniu.carrent.mapper.TNewsMapper;
import com.woniu.carrent.pojo.TNews;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SaCheckLogin
@RestController
@RequestMapping("/tnews")
@CrossOrigin
public class TNewsConntroller {
    @Autowired
    private TNewsMapper tnewsMapper;
    /**
     * 查询新闻
     * @param
     * @return
     */
    @RequestMapping("/selectNews")
    public ResultMessage selectNews(){
        return ResultMessage.success(tnewsMapper.selectList(null));
    }
    /**
     * 发布新闻
     * @param tnews
     * @return
     */

    @RequestMapping("/insertNews")
    public ResultMessage insertNews(@RequestBody TNews tnews){
        tnews.setCreattime(new java.util.Date());
        if(tnewsMapper.insert(tnews)==1){
            return ResultMessage.success("发布成功");
        }
        return ResultMessage.fail("发布失败");
    }
    /**
     * 删除新闻
     * @param tnews
     */
    @RequestMapping("/deleteNews")
    public ResultMessage deleteNews(@RequestBody TNews tnews){
        Integer nid = tnews.getNewsid();
        int flag = tnewsMapper.deleteByNewsId(nid);
        if(flag>0){
            return ResultMessage.success("删除成功");
        }
        return ResultMessage.fail("删除失败");
    }
    /**
     * 修改新闻
     * @param tnews
     * @return
     */
    @RequestMapping("/updateNews")
    public ResultMessage updateNews(@RequestBody TNews tnews){
        int flag = tnewsMapper.updateById(tnews);
        if(flag>0){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 通过newsid查询新闻
     * @param tnews
     * @return
     */
    @RequestMapping("/newdetail")
    public ResultMessage newdetail(@RequestBody TNews tnews){
        TNews tNews = tnewsMapper.selectNews(tnews.getNewsid());
        if(tNews!=null){
            return ResultMessage.success(tNews);
        }
        return ResultMessage.fail("查询失败");
    }
}
